﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GUESTSMANAGEMENT.Controller;
using Coolite.Ext.Web;

namespace GUESTSMANAGEMENT.View.Event
{
    public partial class frmEditRelationship : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public Store GridStore
        {
            get;
            set;
        }


        protected void FormSave(object sender, AjaxEventArgs e)
        {
            try
            {
                string id = this.txt_eventUserId.Text;
                int idUser = int.Parse(this.txt_idUser.Text);
                int idEvent = int.Parse(this.txt_idEvent.Text);
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    gmeventuser eventUser = Controller.EventUser.getByEventUser(int.Parse(id));
                    gmuser objUser = (from user in context.gmuser where user.CDUSER == idUser select user).FirstOrDefault();
                    eventUser.QTBF = int.Parse(this.Nqtbf.Text);
                    eventUser.QTBM = int.Parse(this.Nqtbm.Text);
                    eventUser.QTFF = int.Parse(this.Nqtff.Text);
                    eventUser.QTFM = int.Parse(this.Nqtfm.Text);
                    Controller.EventUser.updateEventUser(eventUser);
                    this.winEditRelationship.Hide();
                    ClearWindow(Controls);
                    this.GridStore.AddScript(string.Format("{0}.reload();", this.GridStore.ClientID));
                    Ext.Msg.Alert("Alerta!", "Números de convites do promoter <br>" + objUser.NMUSER + " </br> foi atualizado com sucesso!").Show();
                    FormStatusBar.SetStatus(new StatusBarStatusConfig { Text = "Números de convites atualizado com sucesso!", IconCls = "", Clear2 = true });

                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public void ShowEventUser()
        {
            this.winEditRelationship.Show();
        }

        public void SetEventUser(gmeventuser eventU, gmuser user, gmevent evento)
        {
            try
            {
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    this.txt_eventUserId.Text = eventU.CDEVENTUSER.ToString();
                    this.userName.Text = user.NMUSER;
                    this.txt_idUser.Text = user.CDUSER.ToString();
                    this.txt_idEvent.Text = evento.CDEVENT.ToString();
                    this.Nqtbf.Text = eventU.QTBF.ToString();
                    this.Nqtbm.Text = eventU.QTBM.ToString();
                    this.Nqtff.Text = eventU.QTFF.ToString();
                    this.Nqtfm.Text = eventU.QTFM.ToString();
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        protected void Close(object sender, AjaxEventArgs e)
        {
            this.winEditRelationship.Hide();
            ClearWindow(Controls);
        }

        public void ClearWindow(ControlCollection AllControls)
        {
            foreach (Control c in AllControls)
            {
                if (c is TextField)
                {
                    ((TextField)c).Text = "";
                }
                if (c is ComboBox)
                {
                    ((ComboBox)c).SetValue("");
                }
                if (c is Checkbox)
                {
                    ((Checkbox)c).Checked = true;
                }
                if (c is DateField)
                {
                    ((DateField)c).SetValue("");
                }
                if (c is TextArea)
                {
                    ((TextArea)c).Text = "";
                    ((TextArea)c).Visible = false;
                }
                if (c is Hidden)
                {
                    ((Hidden)c).Text = "";
                }
                if (c is NumberField)
                {
                    ((NumberField)c).Text = "";
                }

                this.winEditRelationship.Restore();
                ClearWindow(c.Controls);
            }
        }
    }
}